package ${package.Parent}.consumer.${cfg.module}.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Parent}.domain.request.${table.entityPath}.*;
import ${package.Parent}.domain.response.${table.entityPath}.*;
import ${package.Parent}.provider.${cfg.module}.client.I${entity}Client;
import com.laicui.wms.core.response.Result;
import com.laicui.wms.redis.component.RedissonComponent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * ${table.comment!} 控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Tag(name = "${table.comment!}管理")
@RestController
@RequestMapping("/${cfg.module}/${table.entityPath}")
@AllArgsConstructor
public class ${entity}Controller {

    @DubboReference
    private final I${entity}Client ${table.entityPath}Client;

    private final RedissonComponent redissonComponent;

    @GetMapping("/detail/{id}")
    @Operation(description = "${table.comment!}-详情")
    public Result<${entity}DetailResponse> get${entity?replace("Saas", "")}Detail(@PathVariable String id) {
        return Result.OK(${table.entityPath}Client.find${entity}ById(id));
    }

    @GetMapping("/page")
    @Operation(description = "${table.comment!}-分页查询")
    public Result<Page<${entity}PageResponse>> get${entity?replace("Saas", "")}Page(${entity}PageRequest request) {
        return Result.OK(${table.entityPath}Client.find${entity}Page(request));
    }

    @PostMapping("/create")
    @Operation(description = "${table.comment!}-创建")
    public Result<Void> create${entity?replace("Saas", "")}(@RequestBody @Valid ${entity}CreateRequest request) {
        redissonComponent.waitLock("lock_${cfg.module}_${table.entityPath}:", "create", () -> {
            ${table.entityPath}Client.execCreate${entity}(request);
            return null;
        });
        return Result.OK();
    }

    @PostMapping("/update")
    @Operation(description = "${table.comment!}-更新")
    public Result<Void> update${entity?replace("Saas", "")}(@RequestBody @Valid ${entity}UpdateRequest request) {
        redissonComponent.waitLock("lock_${cfg.module}_${table.entityPath}:", "update", () -> {
            ${table.entityPath}Client.execUpdate${entity}(request);
            return null;
        });
        return Result.OK();
    }

    @PostMapping("/delete/{id}")
    @Operation(description = "${table.comment!}-删除")
    public Result<Void> delete${entity?replace("Saas", "")}(@PathVariable String id) {
        redissonComponent.waitLock("lock_${cfg.module}_${table.entityPath}:", "delete", () -> {
            ${entity}DeleteRequest request = new ${entity}DeleteRequest();
            request.setId(id);
            ${table.entityPath}Client.execDelete${entity}(request);
            return null;
        });
        return Result.OK();
    }

    @PostMapping("/delete/batch")
    @Operation(description = "${table.comment!}-批量删除")
    public Result<Void> batchDelete${entity?replace("Saas", "")}s(@RequestBody List<String> ids) {
        redissonComponent.waitLock("lock_${cfg.module}_${table.entityPath}:", "batch_delete", () -> {
            ${entity}BatchDeleteRequest request = new ${entity}BatchDeleteRequest();
            request.setIds(ids);
            ${table.entityPath}Client.execBatchDelete${entity}(request);
            return null;
        });
        return Result.OK();
    }
}